﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iMapa.UI.ModelView.Main;
using iMapa.UI.ModelView.Main.Wizard;
using iMapa.Utils;

namespace iMapa.UI.View.Main.Wizard

{
    /// <summary>
    /// Interaction logic for Ws2ImageFormating.xaml
    /// </summary>
    public partial class Ws2ImageFormating : UserControl, IWizardStep
    {

      

        public Ws2ImageFormating()
        {
            InitializeComponent();
               
        }

        private void btnCropping_Click(object sender, RoutedEventArgs e)
        {
            SelectionAreaViewModel vm = ((NewIMapWizardViewModel)this.DataContext).Selector;
            vm.Mode = SelectionAreaViewModel.FunctionMode.crop;
        }

        private void btnTransform_Click(object sender, RoutedEventArgs e)
        {
            SelectionAreaViewModel vm = ((NewIMapWizardViewModel)this.DataContext).Selector;
            vm.Mode = SelectionAreaViewModel.FunctionMode.transform;
        }

        private void crop_Click(object sender, RoutedEventArgs e)
        {
            NewIMapWizardViewModel vm = (NewIMapWizardViewModel)this.DataContext;
            Rectangle surface = (Rectangle)this.FindName("cropselection");
           
            Image i= (Image)this.FindName("image");
            double xscale = vm.Image.Width  / i.ActualWidth ;
            double yscale = vm.Image.Height/i.ActualHeight ;

            double dpiXconst = vm.Image.DpiX / 92.0;
            double dpiYconst = vm.Image.DpiY / 92.0;

            int xpos = Convert.ToInt32((double)vm.Selector.CropX1 * xscale * dpiXconst);
            int ypos = Convert.ToInt32((double)vm.Selector.CropY1 * yscale *dpiYconst);

            int wdist = (int)(vm.Selector.CropWidth * xscale * dpiXconst);
            int hdist=(int)(vm.Selector.CropHeight * yscale * dpiYconst);
            CroppedBitmap cb;
            try
            {
                 cb = new CroppedBitmap(vm.Image,
                            new Int32Rect(xpos, ypos, wdist, hdist));
            }
            catch
            {
                if (xpos < 0)
                    xpos = 0;
                if (ypos < 0)
                    ypos = 0;
                if (hdist + ypos > vm.Image.PixelHeight)
                    hdist = vm.Image.PixelHeight - ypos;
                if (wdist + xpos > vm.Image.PixelWidth)
                    wdist = vm.Image.PixelWidth - xpos;
                try
                {
                    cb = new CroppedBitmap(vm.Image,
                               new Int32Rect(xpos, ypos, wdist, hdist));
                }
                catch
                {
                    return;
                }
            }
            vm.FinalImage = cb;
            vm.Selector.Mode = SelectionAreaViewModel.FunctionMode.opt0;
            
        }

        private void transform_Click(object sender, RoutedEventArgs e)
        {
            NewIMapWizardViewModel vm = (NewIMapWizardViewModel)this.DataContext;
            Rectangle surface = (Rectangle)this.FindName("cropselection");

            Image i = (Image)this.FindName("image");

            double xscale = (vm.Image.Width / i.ActualWidth) * (vm.Image.DpiX / 92.0);
            double yscale = (vm.Image.Height / i.ActualHeight) * (vm.Image.DpiY / 92.0);

            Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte> source =new Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>(ImgUtils.GetBitmap(vm.Image));
            source =source.WarpPerspective<double>(GetHomographyMatrix(source.Size,xscale,yscale),Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC,Emgu.CV.CvEnum.WARP.CV_WARP_DEFAULT,new Emgu.CV.Structure.Bgr(0,0,0));
            vm.FinalImage = ImgUtils.ToBitmapSource(source);
            vm.Selector.Mode = SelectionAreaViewModel.FunctionMode.opt0;
        }

        private Emgu.CV.Matrix<double> GetHomographyMatrix(System.Drawing.Size size, double xscale, double yscale)
        {
            SelectionAreaViewModel vm = ((NewIMapWizardViewModel)this.DataContext).Selector;
            System.Drawing.PointF[] src = new System.Drawing.PointF[] { new System.Drawing.PointF((float)(vm.X4off*xscale),(float)(vm.Y4off*yscale)),
                                                                       new System.Drawing.PointF((float)(vm.X3off*xscale),(float)(vm.Y3off*yscale)),
                                                                       new System.Drawing.PointF((float)(vm.X2off*xscale),(float)(vm.Y2off*yscale)),
                                                                       new System.Drawing.PointF((float)(vm.X1off*xscale),(float)(vm.Y1off*yscale))};
            System.Drawing.PointF[] dst = ImgUtils.getCornerPoints(size);

            return ImgUtils.CountHomography(src, dst);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            NewIMapWizardViewModel vm = (NewIMapWizardViewModel)this.DataContext;
            vm.Selector.Mode = SelectionAreaViewModel.FunctionMode.opt0;
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            NewIMapWizardViewModel vm = (NewIMapWizardViewModel)this.DataContext;
            vm.FinalImage = null;
            vm.Selector.Mode = SelectionAreaViewModel.FunctionMode.opt0;
        }

        private void updatesize(object sender, SizeChangedEventArgs e)
        {
            NewIMapWizardViewModel vm = (NewIMapWizardViewModel)this.DataContext;
            Image i = (Image)this.FindName("image");
            vm.Selector.initArea(i.ActualWidth, i.ActualHeight);
        }

        #region IWizardStep Members

        public bool IsNextStepValid()
        {
            return true;
        }

        public void SetError()
        {
            return;
        }
        public void Finish()
        {
            throw new NotImplementedException();
        }
        #endregion

        private void upLeft_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NewIMapWizardViewModel vm = (NewIMapWizardViewModel)this.DataContext;
        }
    }
}
